import json

from django.contrib.auth.models import User
from django.db import models


# Create your models here.


class TaskCycle(models.Model):
    """
    任务执行周期，可设定一个周期范围，比如每日每周每月，或者每隔N天
    可设置周期内哪些天是任务可执行时间，scope为全为数字的json数组，表示在该周期内的这些天数可执行任务
    可实现，每月1，3，5号的日期，每月除倒数2，4，6的其他时间
    """
    SCOPE_KIND = ((0, 'Out'), (1, 'In'))
    CYCLE_KIND = (
        ('every', '每日'),
        ('month', '每月'),
        ('week', '每周'),
        ('frequency', '按照频率的天数'),
    )
    CYCLE_STATUS = (
        ('used', '使用中'),
        ('deleted', '已作废'),
    )
    name = models.CharField('周期自定义名称', max_length=250)
    start_time = models.DateField('开始时间')
    frequency = models.IntegerField('周期频率')
    kind = models.CharField('类型', choices=CYCLE_KIND, max_length=25)
    use_scope = models.BooleanField('是否选定范围', default=False)
    inverted_scope = models.BooleanField('范围是否倒排', default=False)
    scope_kind = models.IntegerField(choices=SCOPE_KIND, default=0)
    scope = models.CharField('范围', max_length=250, null=True, blank=True)
    status = models.CharField('状态', choices=CYCLE_STATUS, default='used', max_length=25)
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'task_cycle'
        verbose_name_plural = '任务执行周期'

    @property
    def scope_list(self):
        return json.loads(self.scope)


class TaskLabel(models.Model):
    """
    任务标签，将任务归类，可用于分享
    """
    LABEL_KIND = (
        ('private', '私人的'),
        ('public', '公开的'),
        ('shareable', '分享的'),
    )
    name = models.CharField('标签名', max_length=255)
    desc = models.CharField('标签描述', max_length=500, null=True, blank=True)
    label_kind = models.CharField(choices=LABEL_KIND, default='private', max_length=25)
    share_code = models.CharField(max_length=125, null=True, blank=True)
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'task_label'
        verbose_name_plural = '任务标签'


class Task(models.Model):
    """需要执行的任务
    default_complete,是否默认完成
    """
    TIME_ZONE = (
        ('all', '任意时间'),
        ('early_morning', '清晨'),
        ('morning', '早上'),
        ('before_meridiem', '上午'),
        ('none', '中午'),
        ('after_none', '下午'),
        ('evening', '晚上'),
        ('night', '深夜'),
    )
    name = models.CharField('任务名', max_length=255)
    labels = models.ManyToManyField(TaskLabel, verbose_name='标签')
    desc = models.CharField('任务描述', max_length=500, null=True, blank=True)
    cycle = models.ForeignKey(TaskCycle, verbose_name='任务周期', on_delete=models.DO_NOTHING)
    default_complete = models.BooleanField('是否自动完成', default=True)
    frequency = models.IntegerField('任务在周期内需要完成的次数', default=1)
    consumption_time = models.IntegerField('消耗时间(分)', default=30)
    importance = models.IntegerField('重要程度', default=0)
    priority = models.IntegerField('优先级', default=0)
    execution_time = models.CharField(choices=TIME_ZONE, max_length=125, default='all')
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'task'
        verbose_name_plural = '任务'


class TaskRecording(models.Model):
    """任务执行记录
    默认完成的任务，当未完成时才做记录，非默认完成的任务，每一次需要完成的时候，都要做记录
    """
    task = models.ForeignKey(Task, verbose_name='任务', on_delete=models.DO_NOTHING)
    is_complete = models.BooleanField(default=False)
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'task_recording'
        verbose_name_plural = '任务记录'
